<?php
include_once dirname(dirname(__FILE__)).'/admin-verify.php';
require_once dirname(dirname(dirname(__FILE__))).'/include/db_connect.php';

function getGenerateTable() {
	$table = array();
	for($i=1;$i<=7;$i++) {
		$table[$i] = array();
		foreach(range('a', 'f') as $item) {
			$table[$i][$item]="0";
		}
	}
	$big_duty_cnt = array();
	$user_available_time = array();
	$real_name = array();

	$conn = get_db_conn();
	$sql = 'select id,user_real_name,available_time from user where is_user_on_duty = 1';
	$result = mysql_query($sql,$conn) or die("db connect error");
	while($row = mysql_fetch_array($result)) {
		$big_duty_cnt[$row['id']]=0;
		$real_name[$row['id']]=$row['user_real_name'];
		$_tmp = json_decode($row['available_time'],true);
		$user_available_time[$row['id']] = $_tmp;
		for($i=1;$i<=7;$i++) {
			if($_tmp[$i]['a']=="1"&&$_tmp[$i]['b']=="1")$big_duty_cnt[$row['id']]++;
			if($_tmp[$i]['c']=="1"&&$_tmp[$i]['d']=="1"&&$_tmp[$i]['e']=="1")$big_duty_cnt[$row['id']]++;
			if($_tmp[$i]['f']=="1")$big_duty_cnt[$row['id']]++;
		}
	}

	asort($big_duty_cnt);

	$distribute_big_duty_cnt = array();
	foreach($big_duty_cnt as $key=>$_tmp) {
		$distribute_big_duty_cnt[$key]=0;
		for($i=1;$i<=7&&$distribute_big_duty_cnt[$key]<3;$i++) {
			$last_dis_a = 0;$last_dis_b = 0;
			if($user_available_time[$key][$i]['a']=="1"&&$user_available_time[$key][$i]['b']=="1"&&$table[$i]["a"]=="0"&&$table[$i]["b"]=="0"
				&&$distribute_big_duty_cnt[$key]<3) {
				$user_available_time[$key][$i]['a']="0";
				$user_available_time[$key][$i]['b']="0";
				$table[$i]["a"]=$key;
				$table[$i]["b"]=$key;
				$distribute_big_duty_cnt[$key]++;
				$last_dis_a = 1;
			}
			if($last_dis_a==0&&$user_available_time[$key][$i]['c']=="1"&&$user_available_time[$key][$i]['d']=="1"&&$user_available_time[$key][$i]['e']=="1"
				&&$table[$i]["c"]=="0"&&$table[$i]["d"]=="0"&&$table[$i]["e"]=="0"
				&&$distribute_big_duty_cnt[$key]<3) {
				$user_available_time[$key][$i]['c']="0";
				$user_available_time[$key][$i]['d']="0";
				$user_available_time[$key][$i]['e']="0";
				$table[$i]["c"]=$key;
				$table[$i]["d"]=$key;
				$table[$i]["e"]=$key;
				$distribute_big_duty_cnt[$key]++;
				$last_dis_b = 1;
			}
			if($last_dis_b==0&&$user_available_time[$key][$i]['f']=="1"&&$table[$i]["f"]=="0"&&$distribute_big_duty_cnt[$key]<3) {
				$user_available_time[$key][$i]['f']="0";
				$table[$i]["f"]=$key;
				$distribute_big_duty_cnt[$key]++;
			}
		}
	}

	/*foreach($big_duty_cnt as $key=>$_tmp) {
		for($i=1;$i<=7;$i++) {
			$flag = false;
			if($user_available_time[$key][$i]['a']=="1"&&$table[$i]["a"]=="0") {
				$user_available_time[$key][$i]['a']="0";
				$table[$i]["a"]=$key;
				$flag = true;
			}
			if($user_available_time[$key][$i]['b']=="1"&&$table[$i]["b"]=="0"&&$flag==false) {
				$user_available_time[$key][$i]['b']="0";
				$table[$i]["b"]=$key;
			}
			$flag = false;
			if($user_available_time[$key][$i]['c']=="1"&&$table[$i]["c"]=="0") {
				$user_available_time[$key][$i]['c']="0";
				$table[$i]["c"]=$key;
				$flag = true;
			}
			if($user_available_time[$key][$i]['d']=="1"&&$table[$i]["d"]=="0"&&$flag==false) {
				$user_available_time[$key][$i]['d']="0";
				$table[$i]["d"]=$key;
				$flag = true;
			}
			if($user_available_time[$key][$i]['e']=="1"&&$table[$i]["e"]=="0"&&$flag==false) {
				$user_available_time[$key][$i]['e']="0";
				$table[$i]["e"]=$key;
				$flag = true;
			}
		}
	}*/
	return $table;
}
